/** algorithms.d - Basic algorithms

  Written in the D programming language 1.0

Authors:	Wei Li (oldrev@gmail.com)
Copyright:	Copyright (c) 2007 by Wei Li.
License:	BSD

*/

module dotmars.base.algorithms;

import dotmars.base.iterator;
import dotmars.base.typetraits;


void swap(T)(ref T x, ref T y)
{
	T tmp = x;
	x = y;
	y = tmp;
}

SinglePassReadable find (SinglePassReadable, T) 
	(SinglePassReadable first, SinglePassReadable last, T value)
{ 
	assertCategory!(SinglePassReadable, TraversalCategory.SinglePass, 
			AccessCategory.Readable)();

	while (first != last && first.value != value) ++first;
	return first;
}


SinglePassReadable findIf (SinglePassReadable, Predicate) 
	(SinglePassReadable first, SinglePassReadable last, Predicate pred)
{
	assertCategory!(SinglePassReadable, TraversalCategory.SinglePass, 
			AccessCategory.Readable)();

	while (first != last && pred(first.value)) ++first;
	return first;
}

////////// Copy algorithms //////////////

void copy (SinglePassReadable, IncrementableWritable) 
	(SinglePassReadable first, SinglePassReadable last, IncrementableWritable result)
{
	assertCategory!(SinglePassReadable, TraversalCategory.SinglePass, 
			AccessCategory.Readable)();

	assertCategory!(IncrementableWritable, TraversalCategory.Incrementable, 
			AccessCategory.Writable)();

	for ( ; first != last; ++result, ++first)
		result.value = first.value;

	return result;
}


